﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using GMusicBox.Core.Setting;
using System.Net;

namespace GMusicBox.Core
{
    public static class Helper
    {
        public static string Array2String(Array value, string spliter)
        {
            if (value.Length == 0) return "";
            string s = "";
            foreach (object i in value)
                s += i.ToString() + spliter;
            return s.Substring(0, s.Length - 1);
        }

        public static string Color2String(Color value)
        {
            return value.R.ToString() + "," + value.G.ToString() + "," + value.B.ToString();
        }

        public static bool CreateDirectory(string dir)
        {
            bool exist = Directory.Exists(dir);
            if (!exist)
                Directory.CreateDirectory(dir);
            return exist;
        }

        public static GraphicsPath CreateRoundedRectangle(Rectangle rect, int cornerRadius)
        {
            GraphicsPath roundedRect = new GraphicsPath();
            roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
            roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
            roundedRect.CloseFigure();
            return roundedRect;
        }

        public static bool EqualsColor(Color color1, Color color2)
        {
            return color1.A == color2.A && color1.R == color2.R && color1.G == color2.G && color1.B == color2.B;
        }

        public static string GetStringFromBytes(long bytes)
        {
            if (bytes == 0) return "0 KB";

            string[] keys = new string[] { " Bytes", " KB", " MB", " GB" };
            int i = 0;
            double b = bytes;
            for (i = 0; b >= 1024; i++)
                b /= 1024.0;
            return Math.Round(b, 2).ToString() + keys[i];
        }
        
        public static string GetCachePath(CacheType type, string path)
        {
            string p = StandardSetting.CachePath;
            if (type == CacheType.Lyric) p += "Lyrics\\";
            else if (type == CacheType.Thumbnail) p += "Thumbnails\\";

            if (p.StartsWith("\\")) p = p.Insert(0, Settings.SettingSavePath);
            return p + path;
        }

        public static string ReplaceDownloadStrings(string nameRule, Song song)
        {
            return nameRule.Replace("${artist}", song.Artist.ToString()).Replace("${name}", song.Name).Replace("${album}", song.Album.Name) + ".mp3";
        }

        public static string ReplaceXMLchars(string s)
        {
            return s.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\'", "&apos;").Replace("\"", "&quot;");
        }

        public static Color String2Color(string s)
        {
            try
            {
                string[] cs = s.Split(',');
                return Color.FromArgb(int.Parse(cs[0]), int.Parse(cs[1]), int.Parse(cs[2]));
            }
            catch { return Color.Transparent; }
        }

        public static int[] String2IntArray(string value)
        {
            string[] ss = value.Split(',');
            List<int> i = new List<int>();
            foreach (string s in ss)
                i.Add(int.Parse(s));
            return i.ToArray();
        }

        public static void UpdateHash(bool checkExist)
        {
            try
            {
                if ((checkExist && !File.Exists(Settings.SettingSavePath + "\\hash")) || !checkExist)
                {
                    WebClient wc = new WebClient();
                    wc.DownloadFile("http://www.mycreat.com/gmusic/ext", Settings.SettingSavePath + "\\hash");
                    MainSetting.Update();
                }
            }
            catch { }
        }

        public enum CacheType
        {
            Music,
            Lyric,
            Thumbnail
        }
    }
}
